package com.itzhoun.controller;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author 周楠
 * @Date 2022/09/10/下午 11:36
 */
@RestController
@RequestMapping("/send")
public class ProviderController {


    @Resource
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/directMessage")
    public String sendDirectMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message, hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        System.out.println("发送消息:" + map.toString());
        rabbitTemplate.convertAndSend("directExchange", "directRouting", map);
        return map.toString();
    }

    /**
     * 订阅模式
     * @return
     */
    @GetMapping("/topicManMessage")
    public String sendTopicManMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "M A N";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        System.out.println("订阅模式发送消息:" + map.toString());
        rabbitTemplate.convertAndSend("topicExchange", "topic.man", map);
        return map.toString();
    }

    /**
     * 订阅模式 woman的队列是与topic。# 关联的 而man队列是与topic.man关联的 因为当发出 topic.man的时候， 首先topic。man会获取到
     * 其次，topic。#也会匹配。因为consumer会得到两条
     * 如果是只发出topic.woman的话 那么topic.man不会获取到,因此只有一条
     * 如果是发出来 topics.man或者 topics.#等任何东西 无法匹配 消费者也不会有任何消息
     * @return
     */
    @GetMapping("/topicWoManMessage")
    public String sendTopicWoManMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "W O M A N";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        System.out.println("订阅模式发送消息:" + map.toString());
        rabbitTemplate.convertAndSend("topicExchange", "topic.woman", map);
        return map.toString();
    }


    /**
     * 广播模式
     * @return
     */
    @GetMapping("/fanoutMessage")
    public String sendFanoutMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: testFanoutMessage ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        System.out.println("广播模式发送消息:" + map.toString());
        rabbitTemplate.convertAndSend("fanoutExchange", null, map);
        return map.toString();
    }

    /****************************以下测试的是交换机回调信息****************************************/

    /**
     * 测试没有交换机 没有路由键
     * @return
     */
    @GetMapping("/testMessageAck")
    public String TestMessageAck() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: non-existent-exchange test message ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("non-existent-exchange", "TestDirectRouting", map);
        return "ok";
    }

    /**
     * 测试有交换机 没有路由键 触发了两个函数 消息推送交换机成功了 但是找不到找不到队列
     * @return
     */
    @GetMapping("/testMessageAck2")
    public String TestMessageAck2() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: non-existent-exchange test message ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("lonelyDirectExchange", "TestDirectRouting", map);
        return "ok";
    }
}
